﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class mobile_invite : System.Web.UI.Page
{
    private static string key = "TEST2013";
    string openid = "";
    string enterid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        openid = this.Request.Params["openid"].ToString();
        enterid = this.Request.Params["enterid"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand get_code = new SqlCommand("select IND_IVICODE from INFO_IND where IND_ID=(select IND_ID from IND_CARE where IND_OPENID=@a2 and ENTER_ID=@eid1)",sc);
        get_code.Parameters.AddWithValue("@a2", openid);
        get_code.Parameters.AddWithValue("@eid1", enterid);
        string icode = get_code.ExecuteScalar().ToString();
        label3.Text = icode;
        //如果intro_from不为空，则隐藏label1，textbox1和button1，显示已有的邀请信息
        SqlCommand get_intro = new SqlCommand("select INTRO_FROM from IND_CARE where IND_OPENID=@a1 and ENTER_ID=@eid2",sc);
        get_intro.Parameters.AddWithValue("@a1", openid);
        get_intro.Parameters.AddWithValue("@eid2", enterid);
        object tmp = get_intro.ExecuteScalar();
        if (tmp.ToString()!= "")
        {
            SqlCommand get_introname = new SqlCommand("select IND_NAME from INFO_IND where IND_ID=@iid", sc);
            get_introname.Parameters.AddWithValue("@iid", tmp.ToString());
            label1.Text = "您已经被" + get_introname.ExecuteScalar().ToString() + "邀请来到这个平台啦亲";
            TextBox1.Visible = false;
            Button1.Visible = false;
        }
        sc.Close();
        
    }
    public static string DESdecode(string str)
    {
        try
        {
            byte[] rgbkey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] rgbIV = rgbkey;
            byte[] inputByteArray = Convert.FromBase64String(str);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mstream = new MemoryStream();
            CryptoStream cstream = new CryptoStream(mstream, DCSP.CreateDecryptor(rgbkey, rgbIV), CryptoStreamMode.Write);
            cstream.Write(inputByteArray, 0, inputByteArray.Length);
            cstream.FlushFinalBlock();
            cstream.Close();
            return Encoding.UTF8.GetString(mstream.ToArray());
        }
        catch
        {
            return str;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || TextBox1.Text == null)
        {
            Page.RegisterStartupScript("ss", "<script>alert('请填写所有必需信息！!')</script>");
            return;
        }
        //【判断该用户IND_CARE中的INTRO_FROM是否为空，不为空则跳出】
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand com_fcheck = new SqlCommand("select INTRO_FROM from IND_CARE where IND_OPENID=@a1 and ENTER_ID=@eid3",sc);
        com_fcheck.Parameters.AddWithValue("@a1",openid);
        com_fcheck.Parameters.AddWithValue("@eid3",enterid);
        object tmp = com_fcheck.ExecuteScalar();
        if (tmp.ToString() == "")
        {
            #region 未被邀请
            string pro_id = DESdecode(TextBox1.Text);//邀请人ID,先检验是否存在


            SqlCommand com_check = new SqlCommand("select IND_NAME from INFO_IND where IND_ID=@id", sc);
            com_check.Parameters.AddWithValue("@id", pro_id);
            if (com_check.ExecuteScalar() != null)//邀请码ID存在，判断是否与用户本人ID一致
            {
                SqlCommand com_compare = new SqlCommand();
                com_compare.Connection = sc;
                com_compare.CommandText = "select IND_ID from IND_CARE where IND_OPENID=@a and ENTER_ID=@eid4";
                com_compare.Parameters.AddWithValue("@a", openid);
                com_compare.Parameters.AddWithValue("@eid4", enterid);
                string tmp_id = com_compare.ExecuteScalar().ToString();//获取本人ID
                if (tmp_id.Equals(pro_id) == false)//邀请码ID与本人ID不一致
                { 
                       

                        SqlCommand update = new SqlCommand();
                        update.Connection = sc;
                        update.CommandText = "update IND_CARE set INTRO_FROM=@id where IND_ID=@a and ENTER_ID=@b";
                        update.Parameters.AddWithValue("@id", pro_id);
                        update.Parameters.AddWithValue("@a", tmp_id);
                        update.Parameters.AddWithValue("@b", enterid);
                        while (Application["sqllock"] == "lock")
                        {//检测是否已经锁住               
                            System.Threading.Thread.Sleep(2000);
                        }
                        Application["sqllock"] = "lock";//上锁 
                        
                       
                        if (update.ExecuteNonQuery() == 1)
                        {
                            Application["sqllock"] = "unlock";//解锁 
                            SqlCommand update_crdits = new SqlCommand();
                            update_crdits.Connection = sc;
                            update_crdits.CommandText = "update INFO_IND set IND_CREDITS=IND_CREDITS+50 where IND_ID in(@a,@b)";
                            update_crdits.Parameters.AddWithValue("@a", tmp_id);
                            update_crdits.Parameters.AddWithValue("@b", pro_id);
                            while (Application["sqllock"] == "lock")
                            {//检测是否已经锁住               
                                System.Threading.Thread.Sleep(2000);
                            }
                            Application["sqllock"] = "lock";//上锁 
                            
                            if (update_crdits.ExecuteNonQuery() == 2)
                            {
                                Application["sqllock"] = "unlock";//解锁 
                                Page.RegisterStartupScript("ss", "<script>alert('输入邀请码成功，邀请人和被邀请人同时获得50积分!')</script>");
                                sc.Close();
                                return;

                            }
                            else
                            {
                                Application["sqllock"] = "unlock";//解锁 
                                Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重新输入！!')</script>");
                                sc.Close(); return;
                            }
                        }
                        else
                        {
                            Application["sqllock"] = "unlock";//解锁 
                            Page.RegisterStartupScript("ss", "<script>alert('数据库异常，请重新输入！!')</script>");
                            sc.Close(); return;
                        }

                }
                else
                {
                    Page.RegisterStartupScript("ss", "<script>alert('您输入的邀请码无效，请核实后重新输入！!')</script>");
                    sc.Close(); return;
                }
            }
            else
            {
                Page.RegisterStartupScript("ss", "<script>alert('您输入的邀请码有误，请核实后重新输入！!')</script>");
                sc.Close(); return;
            }
            #endregion
        }
        else
        {
            Page.RegisterStartupScript("ss", "<script>alert('您已经被邀请过啦亲！!')</script>");
            sc.Close(); return;
        }
    }
}